#ifndef MCP2515_H
#define MCP2515_H
#include <List>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <thread>
#include <signal.h>
///#include <MutexLocker>
#include "spi.h"
#include "MCP2515_REG.h"
#include <vector>
///#include <unitrans/datadefine.h>
#include "anbottype.h"
///#include "anbotdef.h"
///#include "qtutil.h"
///#include "StateSemaphore.h"

using namespace std;

#define CAN_DATA_BUF_SIZE 100

///
/// \brief MCP2515功能实现
///
class MCP2515
{
public:
    MCP2515();

    ///
    /// \brief  初始化ＭＣＰ芯片
    /// \return >0:初始化成功
    ///
    int Init(void);

    ///
    /// \brief 解析读取到的数据
    /// \param buf
    ///
    CanDataType ReadBufResolve(const uint8_t *buf);

    ///
    /// \brief 检测CAN芯片是否链接
    /// \retval 已链接
    /// \retval 未链接
    ///
    inline bool IsOpen()
    {
        return spi.isConnected;
    }

    ///
    /// \brief 发送can总线数据
    /// \param data 待发送的数据
    /// \return >0:发送成功
    ///
    int SendCanData(const CanDataType& data);



    ///
    /// \brief 发送can总线数据
    /// \param data 待发送的数据
    /// \return >0:发送成功
    ///
    ///int SendCanDataCommand(const COMDATA& data);


    // test
    int SendCanDataCommand(int id, const char* sendBuf, int size);


    ///
    /// \brief 接收can总线数据的线程
    ///
    bool CanDataReadOnce(CanDataType& dataBuf);


    ///
    /// 发送数据
    ///
    void sendByteData();


    /// 新增
    /// 发送byte数据
    ///
    int WriteBufData(uint8_t* buf);

    Spi spi;
    bool isSpiDeviceOpened;
    bool bSpiReceiveDataFlag;   ///< can总线接收数据标志
};

#endif // MCP2515_H
